class PostModel {
  final String id;
  final String userId;
  final String title;
  final String content; // Markdown格式内容
  final String? coverImage; // 封面图URL
  final LocationInfo location;
  final List<String> tags;
  final int views;
  final List<String> likes; // 点赞用户ID列表
  final List<String> comments; // 关联评论ID
  final DateTime createdAt;
  final DateTime updatedAt;

  PostModel({
    required this.id,
    required this.userId,
    required this.title,
    required this.content,
    this.coverImage,
    required this.location,
    required this.tags,
    this.views = 0,
    this.likes = const [],
    this.comments = const [],
    required this.createdAt,
    required this.updatedAt,
  });

  factory PostModel.fromJson(Map<String, dynamic> json) {
    return PostModel(
      id: json['_id']?.toString() ?? json['id']?.toString() ?? '',
      userId: json['user_id']?.toString() ?? '',
      title: json['title']?.toString() ?? '',
      content: json['content']?.toString() ?? '',
      coverImage: json['cover_image']?.toString(),
      location: LocationInfo.fromJson(json['location'] ?? {}),
      tags: List<String>.from(json['tags'] ?? []),
      views: json['views']?.toInt() ?? 0,
      likes: List<String>.from(json['likes'] ?? []),
      comments: List<String>.from(json['comments'] ?? []),
      createdAt:
          json['created_at'] != null
              ? DateTime.parse(json['created_at'].toString())
              : DateTime.now(),
      updatedAt:
          json['updated_at'] != null
              ? DateTime.parse(json['updated_at'].toString())
              : DateTime.now(),
    );
  }

  Map<String, dynamic> toJson() {
    return {
      '_id': id,
      'user_id': userId,
      'title': title,
      'content': content,
      'cover_image': coverImage,
      'location': location.toJson(),
      'tags': tags,
      'views': views,
      'likes': likes,
      'comments': comments,
      'created_at': createdAt.toIso8601String(),
      'updated_at': updatedAt.toIso8601String(),
    };
  }

  PostModel copyWith({
    String? id,
    String? userId,
    String? title,
    String? content,
    String? coverImage,
    LocationInfo? location,
    List<String>? tags,
    int? views,
    List<String>? likes,
    List<String>? comments,
    DateTime? createdAt,
    DateTime? updatedAt,
  }) {
    return PostModel(
      id: id ?? this.id,
      userId: userId ?? this.userId,
      title: title ?? this.title,
      content: content ?? this.content,
      coverImage: coverImage ?? this.coverImage,
      location: location ?? this.location,
      tags: tags ?? this.tags,
      views: views ?? this.views,
      likes: likes ?? this.likes,
      comments: comments ?? this.comments,
      createdAt: createdAt ?? this.createdAt,
      updatedAt: updatedAt ?? this.updatedAt,
    );
  }

  // 获取点赞数
  int get likesCount => likes.length;

  // 获取评论数
  int get commentsCount => comments.length;

  // 检查用户是否点赞
  bool isLikedBy(String userId) => likes.contains(userId);
}

class LocationInfo {
  final String name;
  final List<double> coordinates; // [经度, 纬度]

  LocationInfo({required this.name, required this.coordinates});

  factory LocationInfo.fromJson(Map<String, dynamic> json) {
    return LocationInfo(
      name: json['name']?.toString() ?? '',
      coordinates:
          json['coordinates'] != null
              ? List<double>.from(json['coordinates'].map((x) => x.toDouble()))
              : [0.0, 0.0],
    );
  }

  Map<String, dynamic> toJson() {
    return {'name': name, 'coordinates': coordinates};
  }
}
